﻿using Sustainalytics.Claims;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Sustainalytics.GCCS.Service.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    //[Authorize]
    //[AutoInvalidateCacheOutput]
    [RoutePrefix("api/v1/gcoverallcompanysummaryextended")]
    public class GCOverallCompanySummaryExtendedController : ApiController
    {
        IGCOverallCompanySummaryExtendedManager _manager;
        IClaimsChecker _claimsChecker;      

        public GCOverallCompanySummaryExtendedController(IGCOverallCompanySummaryExtendedManager manager, IClaimsChecker claimsChecker)
        {
            _manager = manager;
            _claimsChecker = claimsChecker;
        }

       // [CacheOutput(ServerTimeSpan = 100, ClientTimeSpan = 0, NoCache = true, MustRevalidate = true, Private = true)]
        [Route("")]
        public HttpResponseMessage Get([FromUri]GCOverallCompanySummaryExtendedFilters filters, [FromUri]ListOptions listOptions, GCAssessment? overallAssessment = null)
        {
            if (!_claimsChecker.ValidateNonCompliantOrWatchlist(this))
            {
                return this.Request.CreateResponse(HttpStatusCode.Unauthorized);
            }

            List<int> userUniverseIds = GCCSUtility.GetUniverses(overallAssessment);

            listOptions = listOptions ?? ListOptions.Default;

            if (listOptions.GetCount)
            {
                // get count
                return this.Request.CreateResponse(HttpStatusCode.OK, _manager.GetCount(filters, userUniverseIds, overallAssessment));
            }
            else
            {
                // get list
                return this.Request.CreateResponse(HttpStatusCode.OK,
                    _manager.GetList(
                        filters: filters,
                        universeIds: userUniverseIds,
                        overallAssessment: overallAssessment,
                        skip: listOptions.Skip,
                        take: listOptions.Take,
                        orderBy: listOptions.OrderBy,
                        orderByAsc: listOptions.OrderAscending)
                    .Select(el => new GCOverallCompanySummaryExtendedDto().LoadFrom(el))
                );
            }
        }
    }
}
